package com.bookdresser.activities;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.bookdresser.R;

public class ChoosePhotoActivity extends Activity {

	private TextView bt_takephoto, bt_album;
	private Uri uri;
	private String sdState;
	private String picNameStr;
	/***
	 * 使用照相机拍照获取图片
	 */
	public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
	public static final int SELECT_PIC_BY_TACK_PHOTO_RESULT = 2;
	/***
	 * 使用相册中的图片
	 */
	public static final int SELECT_PIC_BY_PICK_PHOTO = 3;
	public static final int SELECT_PIC_BY_PICK_PHOTO_RESULT = 4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_photo);

		sdState = Environment.getExternalStorageState();
		initView();
		initListener();
	}

	private void initView() {
		bt_takephoto = (TextView) this.findViewById(R.id.bt_takephoto);
		bt_album = (TextView) this.findViewById(R.id.bt_album);
	}

	private void initListener() {
		bt_takephoto.setOnClickListener(new TakePhotoListener());
		bt_album.setOnClickListener(new AlbumListener());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return true;
	}

	private class TakePhotoListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {

			if (sdState.equals(Environment.MEDIA_MOUNTED)) {

				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				picNameStr = System.currentTimeMillis() + ".jpg";
				uri = Uri.fromFile(new File(Environment
						.getExternalStorageDirectory(), picNameStr));
				intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
				startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
				System.out.println("-------" + SELECT_PIC_BY_TACK_PHOTO
						+ "------");
			} else {
				Toast.makeText(ChoosePhotoActivity.this, "SD不在",
						Toast.LENGTH_LONG).show();
			}
		}

	}

	private class AlbumListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (sdState.equals(Environment.MEDIA_MOUNTED)) {
				Intent innerIntent = new Intent();
				innerIntent.setAction(Intent.ACTION_GET_CONTENT);
				innerIntent.setType("image/*");
				Intent wrapperIntent = Intent.createChooser(innerIntent, null);
				startActivityForResult(wrapperIntent, SELECT_PIC_BY_PICK_PHOTO);
			} else {
				Toast.makeText(ChoosePhotoActivity.this, "SD不在",
						Toast.LENGTH_LONG).show();
			}
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		Intent intent = getIntent();
		switch (requestCode) {
		case SELECT_PIC_BY_PICK_PHOTO:
			if (data != null) {
				Uri uri = data.getData();
				intent.putExtra("photo", "" + uri);
				setResult(SELECT_PIC_BY_PICK_PHOTO_RESULT, intent);
			}
			break;
		case SELECT_PIC_BY_TACK_PHOTO:
			intent.putExtra("photo", picNameStr);
			System.out.println("-------------photo---------------");
			setResult(SELECT_PIC_BY_TACK_PHOTO_RESULT, intent);
		default:
			break;
		}
		finish();
		// 缩略图 Bitmap bitm ap = data.getParcelableExtra("data");
	}
}
